package com.example.hm_dtfkz.util;

import java.math.BigDecimal;
import java.text.DecimalFormat;

public class NumberFormatUtil {

    public static double getRemainCash(double d1, double d2) {
        BigDecimal bd1 = new BigDecimal(d1);
        BigDecimal bd2 = new BigDecimal(d2);

        return bd1.add(bd2).setScale(4, BigDecimal.ROUND_HALF_EVEN).doubleValue();
    }

    public static String convertDoubleToString(double d) {
        DecimalFormat df = new DecimalFormat("#.###");
        BigDecimal bd = new BigDecimal(String.valueOf(d)).setScale(3, BigDecimal.ROUND_DOWN);
        return df.format(bd);
    }

    public static void main(String[] args) {
        double d1 = 0.4469;
        double d2 = 0.1688;
        String n1 = convertDoubleToString(d1);
        String n2 = convertDoubleToString(d2);
        double remainCash = getRemainCash(d1, d2);
        System.out.println(n1);
        System.out.println(n2);
        System.out.println(remainCash);
    }
}
